﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SCG.SolarVengeanceEngine;
using System.Drawing;
using SCG.WinForms;

namespace SCG.SolarVengeance
{
    public class StarShipListView : ListView
    {
        //events
        public event EventHandler<StarShipTypeEventArgs> StarShipTypeSelected;

        //constructor - default to red color
        public StarShipListView()
            : base()
        {
            FullRowSelect = true;
            HideSelection = false;
            SmallImageList = _images;
            View = View.Details;
            HeaderStyle = ColumnHeaderStyle.None;
            _hdr = Columns.Add("StarShip Type");
        }

        //when color has changed, load StarShip images
        private Color _color = Color.Red;
        public Color Color
        {
            get
            {
                return _color;
            }
            set
            {
                if (_color != value)
                {
                    _color = value;
                    _images.Images.Clear();
                    if (!DesignMode)
                        LoadImages();
                }
            }
        }

        //establish the image dimensions
        public Size ImageSize { get; set; }

        //add a StarShip type
        public void AddStarShipType(StarShipType st)
        {
            LoadImages();
            StarShip dummy = StarShip.CreateInstance(null, null, st, 1, 1, null);
            ListViewItem lvi = Items.Add(dummy.Name);
            lvi.Tag = dummy;
            lvi.ImageIndex = (int)st;
        }

        //add all StarShip types
        public void AddAllStarShipTypes()
        {
            foreach (StarShipType st in StarShip.ShipTypes)
                AddStarShipType(st);
        }

        //does it contain this ship type?
        public bool Contains(StarShipType st)
        {
            foreach (ListViewItem lvi in Items)
                if (lvi.ImageIndex == (int)st)
                    return true;
            return false;
        }

        //select specified ship type
        public void SelectStarShipType(StarShipType st)
        {
            SelectedItems.Clear();
            foreach(ListViewItem lvi in Items)
                if (lvi.ImageIndex == (int)st)
                {
                    lvi.Selected = true;
                    return;
                }
        }

        //synchronize and preserve selected item
        public void SynchronizeStarShips(List<StarShipType> shipTypes)
        {
            bool changed = false;
            int idx = -1;
            if (SelectedItems.Count == 1)
                idx = SelectedItems[0].ImageIndex;

            //add ship types that aren't in the list already
            foreach (StarShipType st in shipTypes)
                if (!Contains(st))
                {
                    changed = true;
                    AddStarShipType(st);
                }

            //remove ship types that aren't in the supplied list
            for (int n = Items.Count - 1; n >= 0; n--)
            {
                StarShipType st = (StarShipType)Items[n].ImageIndex;
                if (!shipTypes.Contains(st))
                {
                    changed = true;
                    Items.RemoveAt(n);
                }
            }

            //re-select
            if (idx >= 0 && changed)
                SelectStarShipType((StarShipType)idx);
        }

        //return the selected ship type
        public StarShipType SelectedStarShipType
        {
            get
            {
                if (SelectedItems.Count != 1)
                    return StarShipType.WarShip;
                else
                    return (StarShipType)SelectedItems[0].ImageIndex;
            }
        }

        //private members
        private ImageList _images = new ImageList();
        private ColumnHeader _hdr;

        //load the starship images if they're not already loaded
        private void LoadImages()
        {
            if (ImageSize.Width == 0)
                return;
            if (_images.Images.Count > 0)
                return;
            _images.ImageSize = ImageSize;
            _hdr.Width = Width - System.Windows.Forms.SystemInformation.VerticalScrollBarWidth - 4;
            foreach (StarShipType st in StarShip.ShipTypes)
            {
                Bitmap bmp = (Bitmap)Bitmap.FromFile("Images\\StarShips\\" + st.ToString() + ".bmp");
                bmp = ImageUtils.ResizeBitmap(bmp, ImageSize.Width, ImageSize.Height, ImageSize.Width == ImageSize.Height);
                bmp = ImageUtils.ColorizeBitmap(bmp, Color, Color.Black);
                _images.Images.Add(bmp);
            }
        }

        //trigger event
        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.OnSelectedIndexChanged(e);
            if (StarShipTypeSelected == null)
                return;
            if (SelectedItems.Count != 1)
                return;
            StarShip ship = (StarShip)SelectedItems[0].Tag;
            StarShipTypeSelected(this, new StarShipTypeEventArgs(ship.ShipType));
        }
    }

    //event type
    public class StarShipTypeEventArgs : EventArgs
    {
        //constructor
        public StarShipTypeEventArgs(StarShipType st)
            : base()
        {
            StarShipType = st;
        }

        //The StarShip type
        public StarShipType StarShipType { get; set; }
    }
}
